library(tidyverse)
library(fixest)
library(showtext)
library(vtable)
library(writexl)
library(haven)
library(gridExtra)

setwd('D:\\WorkingPaper-Series\\Township_Light\\Replication_Files\\workdata')

##################################
########## Figure C4 #############
##################################

mat_spillover <- read_delim('mat_spillover.csv')
# Panel A
mat_spillover %>% mutate(outcome = if_else(outcome==1,'Log(light+1)','IHS(light)')) %>% 
  mutate(upper_ci = coef + 1.96*se, lower_ci = coef - 1.96*se) %>% 
  ggplot(aes(dist, coef,group = outcome, color = outcome))+
  geom_hline(yintercept = 0,color = '#D80032', lwd = 0.5, lty = 2)+
  geom_point(aes(shape = outcome, color = outcome, fill = outcome),position=position_dodge(3))+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width=.2, lwd = .3,position=position_dodge(3))+
  theme_bw()+
  theme(legend.position = c(0.15,0.85),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.45, r = 0.1, b = 0, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 10),
        axis.text =  element_text(family = 'serif',size = 8))+
  labs(y = '', x = 'Distance to TBV') +
  scale_y_continuous(limits = c(-0.07,0.03))+
  scale_color_manual(values = c('Log(light+1)' = 'black', 'IHS(light)' = 'grey50')) +
  scale_fill_manual(values = c('Log(light+1)' = 'black', 'IHS(light)' = 'grey50')) +
  scale_shape_manual(values = c('Log(light+1)' = 21, 'IHS(light)' = 23)) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))


mat_neighbor <- read_delim('mat_neighbor.csv')
# Panel B
mat_neighbor %>% mutate(outcome = if_else(outcome==1,'Log(light+1)','IHS(light)')) %>% 
  mutate(upper_ci = coef + 2*se, lower_ci = coef - 2*se) %>% 
  ggplot(aes(dist, coef,group = outcome, color = outcome))+
  geom_hline(yintercept = 0,color = '#D80032', lwd = 0.5, lty = 2)+
  geom_point(aes(shape = outcome, color = outcome, fill = outcome),position=position_dodge(3))+
  geom_errorbar(aes(ymin = lower_ci, ymax = upper_ci), width=.2, lwd = .3,position=position_dodge(3))+
  theme_bw()+
  theme(legend.position = c(0.15,0.85),
        legend.background = element_rect(color = 'black', size = 0.1,linewidth = 0.1, fill = 'white'),
        legend.margin = margin(t = -0.45, r = 0.1, b = 0, l = 0.1, unit = "cm"),
        text = element_text(family = 'serif',size = 10),
        axis.text =  element_text(family = 'serif',size = 8))+
  labs(y = '', x = 'Distance of Spillover') +
  scale_y_continuous(limits = c(-0.03,0.06))+
  scale_color_manual(values = c('Log(light+1)' = 'black', 'IHS(light)' = 'grey50')) +
  scale_fill_manual(values = c('Log(light+1)' = 'black', 'IHS(light)' = 'grey50')) +
  scale_shape_manual(values = c('Log(light+1)' = 21, 'IHS(light)' = 23)) +
  guides(colour = guide_legend(""),fill = guide_legend(""),shape = guide_legend(""))